<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
use think\facade\Cache;
class Article extends Controller {
	
	public function index(){
		return $this->fetch();
	}
	
	
	
	public function reduction(){
		$id = request()->param('aid');
		$ids = explode(",",rtrim($id, ','));
		$value = request()->param('value');
		$table = request()->param('table');
		$ndata = ['status'=> $value];
		$result = Db::name($table)->where('aid','in',$ids)->update($ndata);
		return show(0, 'OK', $result, 200);
	}
	
	public function delete_status(){		
		$id = request()->param('aid');
		$model_id = request()->param('model_id');
		$ids = explode(",",rtrim($id, ','));
		$table = request()->param('table');
		$ndata = ['status'=> 0];
		$result = Db::name($table)->where('aid','in',$ids)->update($ndata);
		if($result){
			return show(0, 'OK', $result, 200);
		}else{
			return show(1, "删除失败", "", 200);
		}		
	}
	
	public function lists(){
		$id = request()->param('id');
		$model_id= request()->param('model_id');
		$data = db('models')->where(['id'=>$model_id])->find();
		$this->assign('id',$id);
		$this->assign('model_id',$model_id);
		$this->assign('table',$data['table_name']);
		if($data['ispage']){
			$tablec = $data['table_name'].'_data';
			$content = db($data['table_name'])
						->alias("a")
						->join($tablec." b","a.did=b.id")
						->where(['cid'=>$id,'status'=>1])
						->find();
			$modelField = db("models_field")->where(['model_id'=>$model_id,'show'=>1])->order('sort')->select();
			if(empty($content)){
				$content = [];
				foreach($modelField as $key=>$value){
					$content[$value['ename']] = "";
				}
				$content['aid'] = "";
			}
			$this->assign('data', $modelField);
			$this->assign('content', $content);
			// p($content);die;
			return $this->fetch('page');
		}
		// p($data);die;
		$this->assign('model',$data);
		return $this->fetch();
	}
	
	//栏目列表
	public function cate() {
		$cate = db('category')->where('model_id','>',0)->order('sort','asc')->column('id,pid,name,model_id');
		$data = [];
		$j = 0;
		foreach($cate as $key=>$v){
			$data[$j]['id'] = $v['id'];
			$data[$j]['pid'] = $v['pid'];
			$data[$j]['pId'] = $v['pid'];
			$data[$j]['name'] = $v['name'];
			$data[$j]['model_id'] = $v['model_id'];
			$data[$j]['open'] = true;
			$j++;
		}
	    return show(config('code.success'), 'OK', $data, 200);
	}
	//文章列表
	public function article_data(){
		$data = request()->param();
		$id = request()->param('id');
		$limit = request()->param('limit');
		$table = request()->param('table');
		$page = request()->param('page');
		$start = ($page-1)*$limit;
		$title = isset($data['title'])?$data['title']:'';
		
		$d = $this->getCateData($id);
		$count = Db::name($table)->where('cid','in',$d)->where(['status'=>1])->count();
		$result = Db::name($table)->where('cid','in',$d)->where(['status'=>1])->limit($start,$limit)->order('aid desc,sort asc')->select();
		if($title){
			$count = Db::name($table)->where('cid','in',$d)->where(['status'=>1])->whereLike('title',"%$title%")->count();
			$result = Db::name($table)->where('cid','in',$d)->where(['status'=>1])->limit($start,$limit)->order('aid desc,sort asc')->whereLike('title',"%$title%")->select();
		}
		return showTable(0, 'OK', $count ,$result, 200);
	}
	
	//编辑
	public function art_edit(){
		if(request()->isPost()){
			$data = request()->param();
			// p($data);die;
			$tableName = db('models')->where(['id'=>$data['model_id']])->field('id,table_name,ispage')->find();
			$prefix=config('database.prefix');
			
			
			//主文章表名称
			$table=$prefix.$tableName['table_name'];
			$ntable = $tableName['table_name'];
			$_columns=Db::query("SHOW COLUMNS FROM {$table}");
			//附表
			$tabledata = $prefix.$tableName['table_name'].'_data';
			$tabledataname = $tableName['table_name'].'_data';
			$_columnsD=Db::query("SHOW COLUMNS FROM {$tabledata}");
			
			$columnsD = [];
			foreach ($_columnsD as $v) {
			    $columnsD[]=$v['Field'];
			}
			$ndataD = [];
			foreach ($data as $k => $v) {
			    if(in_array($k, $columnsD)){
			        if(is_array($v)){
			            $v=implode(',', $v);
			        }
			        $ndataD[$k]=$v;
			    }
			}
			$columns = [];
			
			foreach ($_columns as $v) {
			    $columns[]=$v['Field'];
			}
			$ndata=[];
			foreach ($data as $k => $v) {
			    if(in_array($k, $columns)){
			        if(is_array($v)){
						// p($v);die;
			            $v=implode(',', $v);
			        }
			        $ndata[$k]=$v;
			    }
			}
			$content = db($tableName['table_name'])->where('aid','=',$data['aid'])->find();
			// p($content);die();
			if(!empty($content)){
				unset($ndata['aid']);
				db($tabledataname)->where('id',$content['did'])->update($ndataD);
				$result = db($ntable)->where('aid',$content['aid'])->update($ndata);
				return show(0, '保存成功', $result, 200);
			}
		}
		
		
		$cid = request()->param('cid');
		$aid = request()->param('aid');
		
		$model_id= request()->param('model_id');
		$this->assign('cid',$cid);
		$this->assign('aid',$aid);
		$this->assign('model_id',$model_id);
		$data = db('models')->where(['id'=>$model_id])->find();
		
		
		
		
		$tablec = $data['table_name'].'_data';
		$content = db($data['table_name'])
					->alias("a")
					->join($tablec." b","a.did=b.id")
					->where('aid','=',$aid)
					->find();
		$modelField = db("models_field")->where(['model_id'=>$model_id,'show'=>1])->order('sort')->select();
		$this->assign('data', $modelField);
		$this->assign('content', $content);
		$this->assign('aid',$aid);
		$this->assign('model_id',$model_id);	
		return $this->fetch();
	}
	
	
	//推荐
	public function push(){
		$id = request()->param('aid');
		$value = request()->param('value');
		$table = request()->param('table');
		$ndata = ['istop'=>$value];
		$result = Db::name($table)->where('aid', $id)->update($ndata);
		return show(0, 'OK', $result, 200);
	}
	//显示
	public function show(){
		$id = request()->param('aid');
		$value = request()->param('value');
		$table = request()->param('table');
		$ndata = ['show'=>$value];
		$result = Db::name($table)->where('aid', $id)->update($ndata);
		return show(0, 'OK', $result, 200);
	}
	
	
	//添加页面的字段列表
	public function fields_list(){
		$param = request()->param();
		$id = request()->param('id');
		$model_id = request()->param('model_id');
		$data = db("models_field")->where(['model_id'=>$model_id,'show'=>1])->order('sort')->select();
		$content = [];
		$this->assign('id',$id);
		$this->assign('model_id',$model_id);
		$this->assign('data',$data);
		$this->assign('content', $content);
		return $this->fetch();
	}
	//添加数据
	public function add_data(){
		$data = request()->param();
		// p($data);die;
		$tableName = db('models')->where(['id'=>$data['model_id']])->field('id,table_name,ispage')->find();
		$prefix=config('database.prefix');
		
		
		//主文章表名称
		$table=$prefix.$tableName['table_name'];
		$ntable = $tableName['table_name'];
		$_columns=Db::query("SHOW COLUMNS FROM {$table}");
		//附表
		$tabledata = $prefix.$tableName['table_name'].'_data';
		$tabledataname = $tableName['table_name'].'_data';
		$_columnsD=Db::query("SHOW COLUMNS FROM {$tabledata}");
		
		$columnsD = [];
		foreach ($_columnsD as $v) {
		    $columnsD[]=$v['Field'];
		}
		$ndataD = [];
		foreach ($data as $k => $v) {
		    if(in_array($k, $columnsD)){
		        if(is_array($v)){
		            $v=implode(',', $v);
		        }
		        $ndataD[$k]=$v;
		    }
		}
		$columns = [];
		
		foreach ($_columns as $v) {
		    $columns[]=$v['Field'];
		}
		$ndata=[];
		foreach ($data as $k => $v) {
		    if(in_array($k, $columns)){
		        if(is_array($v)){
					// p($v);die;
		            $v=implode(',', $v);
		        }
		        $ndata[$k]=$v;
		    }
		}
		
		if($tableName['ispage']){
			$content = db($tableName['table_name'])->where('cid','=',$data['cid'])->find();
			
			if(!empty($content)){
				db($tabledataname)->where('id',$content['did'])->update($ndataD);
				$result = db($ntable)->where('aid',$content['aid'])->update($ndata);
				return show(0, '保存成功', $result, 200);
			}else{
				$did = db($tabledataname)->insertGetId($ndataD);
				$ndata['did'] = $did;
				$ndata['status'] = 1;
			}
		}else{
			$did = db($tabledataname)->insertGetId($ndataD);
			$ndata['did'] = $did;
			$ndata['status'] = 1;
		}
		
		$result = db($ntable)->insert($ndata);
		return show(0, '添加成功', $result, 200);
		
	}
	//搜索文章
	public function seach(){
		if(request()->isPost()){
			$data = request()->param();
			$table = $data['table'];
			$title = $data['title'];
			$istop = $data['istop'];
			$id = $data['id'];
			$d = $this->getCateData($id);
			$count = db($table)->where('cid','in',$d)->where(['status'=>1])->whereLike('title',"%$title%")->count();
			$result = db($table)->where('cid','in',$d)->where(['status'=>1])->whereLike('title',"%$title%")->select();
			// p($result);die();
			return showTable(0, 'OK', $count ,$result, 200);
		}
	}
	
	
	//处理所属分类
	public function getCateData($id)
	{
		$data = db('category')->select();
	    //1.先找出所有子集
	    $ids = $this->getSon($data, $id);
	    //2.把自己追加进去
	    $ids[] = $id;
	    //3.获取除了自己和自己子集的全部数据
	    // $map['id'] = array('not in', $ids);
	    // $data = db('category')->where($map)->select();
		// var_dump($ids);die;
	    return $ids;
	}
	
	//递归找子集
	public function getSon($data, $id)
	{
	    static $temp = [];
	    foreach ($data as $k => $v) {
	        if ($v['pid'] == $id) {//这个时候说明你是$cid的儿子
	            $temp[] = $v['id'];
	            $this->getSon($data, $v['id']);
	        }
	    }
	    return $temp;
	}
	
}